<?php
/**
 * 提供给其他地三方使用API接口
 * @author coldarmy
 * **/
apf_require_class('Bll_Attendance_UserInfo');
apf_require_class('Bll_Attendance_OrgRelation');
apf_require_class('APF_Controller');

apf_require_class('Bll_Hr_OutInterface');

class Oa_Attendance_UserAPITestController extends APF_Controller {

    private $bll_user;
    private $bll_org;

    public function handle_request() {
        $request = APF::get_instance()->get_request();
        $params = $request->get_parameters();
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_org = new Bll_Attendance_OrgRelation();
        $params['version'] = empty($params['version']) ? 0 : 1;

        echo '新接口中如果员工之前已经离职，就没有部门和职位信息<br>';
        echo '左侧为新接口输出 右侧为老接口<br>';
        echo '<form name="f" method="post">
                <select name="columns">
                <option value="name">用户名</option>
                <option value="code">员工号</option>
                <option value="email">email</option>
                <option value="user_id">user_id</option>
                </select>
                <input type="text" name="keyword" value="'.$_POST['keyword'].'">
                <input type="checkbox" name="superior" value="1" checked>查看上级
                <input type="checkbox" name="subordinate" value="1" checked> 查看下级
                <input type="submit" name="s" value="Search">
                </form>';
        if($_POST){
            $hr_echo = $this->_get_user_by_hroa($params);
            $oa_echo = $this->get_user_info($params);
            //print_r($hr_echo);
            //print_r($oa_echo);
            echo '<table width="900" border="1"><tr>';
            echo '<td><table width="200" ><td>基本信息</td></tr><tr><td>上级</td></tr><tr><td>下级</td></tr></table></td>';
            foreach($hr_echo as $val){
            echo '<td><font color="red">新接口</font><table width="250" border="1"><td>用户名:'.$val['name'].'<br>工号:'.$val['code'].'<br>邮箱:'.$val['email'].'<br>部门:'.$val['city_name'].$val['department_name'].'<br>职位:'.$val['job_name'].'<br></td></tr>';
            echo '<tr><td>上级<br>';
            if(!empty($val['superior'])){
                echo $val['superior']['user_id'].'=='.$val['superior']['name'].'=='.$val['superior']['code'];
            }else{
                echo '无上级';
            }
            echo '</td></tr>';
            echo '<tr><td>下级('.count($val['subordinate']).')<br>';
            if(!empty($val['subordinate'])){
                foreach($val['subordinate'] as $value){
                    echo $value['user_id'].'=='.$value['name'].'=='.$value['code']."<br>";
                }
            }else{
                echo '无下级';
            }
            echo '</td></tr>';
            echo '</table></td>';
            }
            foreach($oa_echo as $val){
            echo '<td><table width="250" border="1"><td>用户名:'.$val['name'].'<br>工号:'.$val['code'].'<br>邮箱:'.$val['email'].'<br>部门:'.$val['city_name'].$val['department_name'].'<br>职位:'.$val['job_name'].'<br></td></tr>';
            echo '<tr><td>上级<br>';
            if(!empty($val['superior'])){
                echo $val['superior']['user_id'].'=='.$val['superior']['name'].'=='.$val['superior']['code'];
            }else{
                echo '无上级';
            }
            echo '</td></tr>';
            echo '<tr><td>下级('.count($val['subordinate']).')<br>';
            if(!empty($val['subordinate'])){
                foreach($val['subordinate'] as $value){
                    echo $value['user_id'].'=='.$value['name'].'=='.$value['code']."<br>";
                }
            }else{
                echo '无下级';
            }
            echo '</td></tr>';
            echo '</table></td>';
            }
            echo '</tr></table>';
        }

    }
    /**
     * HROA系统数据
     * @param unknown_type $params
     * @return multitype:|Ambigous <multitype:, unknown>
     */
    private function _get_user_by_hroa($params){
        $result = array();
        if(empty($params) || empty($params['keyword'])) {
            return $result;
        }
        $auth = $params['auth'];
        $keyword = $params['keyword'];
        $superior = isset($params['superior'])? $params['superior']:0;
        $subordinate = isset($params['subordinate'])? $params['subordinate']:0;
        $sys_rsa = 'ajkzwww';
        $check_auth = md5(md5($keyword).$sys_rsa);
        //验证auth
        if($auth != $check_auth) {
            //return array('验证错误！');
        }
        $bll = new Bll_Hr_OutInterface();
        $columns = explode(',',$params['columns']);
        $data = $bll->search_user($keyword,$columns,$superior,$subordinate);
        return $data ;
    }
    private function get_user_info($params) {
        $result = array();
        if(empty($params) || empty($params['keyword'])) {
            return $result;
        }
        $auth = $params['auth'];
        $keyword = $params['keyword'];
        $superior = isset($params['superior'])? $params['superior']:0;
        $subordinate = isset($params['subordinate'])? $params['subordinate']:0;
        $sys_rsa = 'ajkzwww';
        $check_auth = md5(md5($keyword).$sys_rsa);
        //验证auth
        if($auth != $check_auth) {
            //return array('验证错误！');
        }

        $columns = explode(',',$params['columns']);
        if(in_array('user_id',$columns)){
            $where['user_id'] = $keyword;
        }else{
            $where[$this->build_where($columns,$keyword)] = 1;
        }
        $tmp = $this->bll_user->get_user_by_condition($where);
        if(empty($tmp)) {
            return $result;
        }
        //封装数据
        foreach ($tmp as $key => $val) {
            $result[$key]['user_id'] = $val['user_id'];
            $result[$key]['name'] = $val['name'];
            $result[$key]['code'] = $val['code'];
            $result[$key]['email'] = $val['email'];
            $result[$key]['mobile'] = $val['mobile'];
            $result[$key]['enter_date'] = $val['enter_date'];
            $result[$key]['indue_date'] = $val['indue_date'];
            $result[$key]['dimission_date'] = $val['dimission_date'] == '0000-00-00 00:00:00'?0:$val['dimission_date'];
            $result[$key]['english_name'] = $val['english_name'];
            $result[$key]['department_name'] = $val['department_name'];
            $result[$key]['job_name'] = $val['position_name'];
            $result[$key]['city_name'] = $val['city_name'];
            if($superior) {
                $result[$key]['superior'] = $this->get_superior($val['user_id']);
            }
            if($subordinate) {
                $result[$key]['subordinate'] = $this->get_subordinate($val['user_id']);
            }
            $result[$key]['city_name'] = $val['city_name'];
        }
        return $result;
    }

    /**
     * 获取下属信息
     * @param int $user_id
     * @return array
     * **/
    private function get_subordinate($user_id) {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $tmp = $this->bll_org->get_subordinate_by_user_id($user_id);
        if(empty($tmp)) {
            return $result;
        }
        $ids =  array();
        foreach ($tmp as $key => $val) {
            $ids[] = $val['user_id'];
        }
        $back = $this->bll_user->get_user_by_ids($ids);
        if(empty($back)) {
            return $result;
        }
        foreach ($back as $k => $v) {
            $result[$k]['user_id'] = $v['user_id'];
            $result[$k]['name'] = $v['name'];
            $result[$k]['code'] = $v['code'];
        }
        return $result;
    }

    /**
     * 获取上级信息
     * **/
    private function get_superior($user_id) {
         $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $tmp = $this->bll_org->get_sup_id($user_id,1);
        $tmp = $this->bll_user->get_user_by_ids($tmp);
        if (empty($tmp)) {
            return $result;
        }
        $result['name'] = $tmp[0]['name'];
        $result['code'] = $tmp[0]['code'];
        $result['user_id'] = $tmp[0]['user_id'];
        return $result;
    }

    /**
     * 构建查询条件
     * @param array $data
     * @param string $keyword
     * @return string
     * **/
    private function build_where($data,$keyword) {
        $result = ' 1 != ? ';
        if(empty($data) || empty($keyword)) {
            return $result;
        }
        foreach ($data as $key => $val) {
            if(in_array($val,array('code','name','email'))) {
                $result .= ' OR '.$this->build_columns_condition($val,$keyword);
            }
            if($val == 'user_id') {
                $result = $this->build_columns_condition($val,$keyword);
                break;
            }
        }
        return $result;
    }

    /**
     * 构建字段查询的组合条件
     * @param string $columns
     * @param string $keyword
     * @return string
     * **/
    private function build_columns_condition($columns,$keyword) {
        $result = '';
        if($columns == 'code') {
            $result = " code = '{$keyword}' ";
        } elseif ($columns == 'name') {
            $result = " name like '%".$keyword."%' ";
        } elseif ($columns == 'email') {
            $result = " email like '%".$keyword."%' ";
        } elseif ($columns == 'user_id') {
            //$result = " user_id = '{$keyword}' ";
        }
        return $result;
    }


}
